********************************************************************************
* regout.ado
* This function wraps the regressions and stores estimates as well as control group means of the dependent variable

capture program drop regout
program define regout, eclass // declare the program an estimation command
	version 12.1
	tempvar _r
	syntax varlist(numeric fv) [if] [, cluster(string) estname(string) treat(string)]
	marksample touse
	replace `touse' = `touse'==1 & `treat'<.
	quietly count if `touse'
	if (`r(N)'==0) error 2000
	
	tempvar resid
	gettoken depvar indepvar: varlist		// separate the dependent from the independent variables
	quietly regress `depvar' `indepvar' if `touse'
	predict `_r' if `touse', residuals
	
	regress `depvar' `treat' `indepvar' if `touse', vce(cluster rno) // regress always clusters on group level
	sum `depvar' if `touse' & `treat' ==0, meanonly 
	estadd scalar base = r(mean)
	eststo `estname'
	
	/* //ritest `treat' _b[`treat'], strata(ksa) cluster(`cluster') reps($reps): regress `_r' `treat' if `touse'
	matrix pval = r(p)
	matrix colnames pval = "`treat'"
	estadd scalar pval = pval[1,1]: `estname'
	estadd matrix pvalm = pval: `estname'
	estimates restore `estname' */
end
